#include "qcircle.h"
#include "point.h"

#include <QPaintEvent>
#include <QPainter>
#include <QStyleOption>
#include <QStylePainter>


QCircle::QCircle(QWidget *parent) :
    QWidget(parent), QBody(new Circle(0)), circle(static_cast<Circle*>(body))
{
    setMinimumSize(50, 50);
    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
}

void QCircle::paintEvent(QPaintEvent *e)
{
    QWidget::paintEvent(e);
    QStyleOption opts;
    opts.init(this);
    QStylePainter stylePainter(this);
    stylePainter.drawPrimitive(QStyle::PE_Widget, opts);
    QPainter p(this);
    p.setBrush(QBrush(Qt::red));
    p.drawEllipse(QPointF(circle->pX(), circle->pY()), circle->radius(), circle->radius());
}

void QCircle::resizeEvent(QResizeEvent *e)
{
    QWidget::resizeEvent(e);
    const QSize s = e->size() / 2;

    circle->radius(std::min(s.width(), s.height())-1);
    position(s.width(), s.height());
}

void QCircle::mouseDoubleClickEvent(QMouseEvent *e)
{
    Point pt(e->x(), e->y());
    vector3 ignoreV;
    scalar ignoreS;
    if (intersect(pt, *circle, ignoreV, ignoreS, pt.position() - circle->position()))
        emit dblClicked(circle);
}
